/**
 * 
 */
package com.manage.action;

import java.util.Calendar;
import java.util.List;

import org.apache.log4j.Logger;


import com.action.AbstractAction;
import com.manage.cache.ChannelManager;
import com.manage.model.Channel;
import com.util.Util;

/**
 * @author jinmei.wang
 *
 */
public class ChannelAction extends AbstractAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private Logger logger;
	
	public ChannelAction()
	{
		logger = Logger.getLogger(this.getClass());
	}
	
	public String execute()
	{
		return SUCCESS;
	}

	private List<Channel> channels;
	private Channel       channel;
	private String        type;
	
	public String list()
	{
		logger.debug("list account");
		channels = ChannelManager.getInstance().getChannels();
		return SUCCESS;
	}
	
	public String show()
	{
		if (channel.getId() >= 0)
		{
			channel = ChannelManager.getInstance().getChannel(channel.getId());
			type = "update";
		}
		else
		{
			channel.setId(-1);
			Calendar cal = Calendar.getInstance();
			channel.setContractDate(Util.dateToString(cal.getTime(),"yyyy-MM-dd"));
			cal.add(Calendar.YEAR, 1);
			channel.setContractEnd(Util.dateToString(cal.getTime(),"yyyy-MM-dd"));
			type = "insert";
		}

		return SUCCESS;
	}
	
	private String contractDate;
	private String contractEnd;
		
	public String save()
	{
		channel.setContractDate(contractDate);
		channel.setContractEnd(contractEnd);
		ChannelManager.getInstance().saveChannel(operator, type, channel);
		return SUCCESS;
	}

	/**
	 * @return the channels
	 */
	public List<Channel> getChannels() {
		return channels;
	}

	/**
	 * @param channels the channels to set
	 */
	public void setChannels(List<Channel> channels) {
		this.channels = channels;
	}

	/**
	 * @return the channel
	 */
	public Channel getChannel() {
		return channel;
	}

	/**
	 * @param channel the channel to set
	 */
	public void setChannel(Channel channel) {
		this.channel = channel;
	}

	/**
	 * @return the type
	 */
	public String getType() {
		return type;
	}

	/**
	 * @param type the type to set
	 */
	public void setType(String type) {
		this.type = type;
	}

	/**
	 * @return the contractDate
	 */
	public String getContractDate() {
		return contractDate;
	}

	/**
	 * @param contractDate the contractDate to set
	 */
	public void setContractDate(String contractDate) {
		this.contractDate = contractDate;
	}

	/**
	 * @return the contractEnd
	 */
	public String getContractEnd() {
		return contractEnd;
	}

	/**
	 * @param contractEnd the contractEnd to set
	 */
	public void setContractEnd(String contractEnd) {
		this.contractEnd = contractEnd;
	}
	
}
